//
//  YRecommendeBookDetailViewController.m
//  Reader
//
//  Created by yuemei on 2019/5/22.
//  Copyright © 2019 SF. All rights reserved.
//

#import "YRecommendeBookListDetailVC.h"
#import "YNetworkManager.h"
#import "YRecommendeBookListDetailHeaderView.h"
#import "YRecommendBookListDetailModel.h"
#import "YRecommendeBookListDetailCell.h"
#import "YRecommendListDetailBookSubModel.h"
#import "YBookDetailViewController.h"


@interface YRecommendeBookListDetailVC ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSURLSessionTask *recommendListTask;
@property (strong, nonatomic) YNetworkManager *netManager;
@property (nonatomic, strong) YRecommendBookListDetailModel *model;
@end

@implementation YRecommendeBookListDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    _netManager = [YNetworkManager shareManager];
    [self.tableView setBackgroundColor:HexColor(0xf7f7f7, 1)];
    [self.tableView registerClass:[YRecommendeBookListDetailCell class] forCellReuseIdentifier:@"YRecommendeBookListDetailCell"];
    [self.tableView registerClass:[YRecommendeBookListDetailHeaderView class] forHeaderFooterViewReuseIdentifier:@"YRecommendeBookListDetailHeaderView"];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self getNetRecommendListData];
}

- (void)getNetRecommendListData{
    
    __weak typeof(self) wself = self;
    _recommendListTask = [_netManager getWithAPIType:YAPITypeRecommendBookListDetail parameter:self.bookListID success:^(id response) {
        self.model = response;
        [self.tableView reloadData];
        DDLogInfo(@"bookDetail %@",response);
    } failure:^(NSError *error) {
        if (error.code == -999) {
            DDLogInfo(@"getNetBookDetailData detailTask cancel");
        } else {
            DDLogError(@"YAPITypeBookDetail %@ error ",wself.bookListID);
            [YProgressHUD showErrorHUDWith:error.localizedFailureReason];
            [wself.navigationController popViewControllerAnimated:YES];
        }
        
    }];
}



- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    YRecommendeBookListDetailHeaderView *headerView = [YRecommendeBookListDetailHeaderView new];
    headerView.model = self.model;
    return headerView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.model.books.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YRecommendeBookListDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YRecommendeBookListDetailCell"];
    cell.model = self.model.books[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    YRecommendListDetailBookSubModel *model = self.model.books[indexPath.row];
    return 123 + [self getLableSideWithText:model.comment TextFont:[UIFont systemFontOfSize:13] MaxWidth:kScreenWidth - 36 MaxHeight:CGFLOAT_MAX];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 15+22+15+15+[self getLableSideWithText:self.model.desc TextFont:[UIFont systemFontOfSize:12] MaxWidth:kScreenWidth - 36 MaxHeight:CGFLOAT_MAX]+40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    YBookDetailViewController *detailVC = [[YBookDetailViewController alloc] init];
    YRecommendListDetailBookSubModel *model = self.model.books[indexPath.row];
    detailVC.selectBook = model.book;
    [self.navigationController pushViewController:detailVC animated:YES];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (CGFloat)getLableSideWithText:(NSString *)text TextFont:(UIFont *)textFont  MaxWidth: (CGFloat)maxWidth MaxHeight:(CGFloat)maxHeight{
    if ([text isNotBlank]) {
        NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
        [attributes setObject:textFont forKey:NSFontAttributeName];
        NSAttributedString *aStr = [[NSAttributedString alloc] initWithString:text attributes:attributes];
        YYTextContainer *container = [YYTextContainer new];
        container.size = CGSizeMake(maxWidth, maxHeight);
        container.maximumNumberOfRows = 0;
        YYTextLayout *layout = [YYTextLayout layoutWithContainer:container text:aStr];
        if(maxWidth == CGFLOAT_MAX){
            return ceil(layout.textBoundingSize.width);
        }
        if(maxHeight == CGFLOAT_MAX){
            return ceil(layout.textBoundingSize.height);
        }
    }
    
    return 0;
}


- (IBAction)backVC:(id)sender {
    if (self.recommendListTask.state == NSURLSessionTaskStateRunning) {
        [self.recommendListTask cancel];
    }
    [YProgressHUD hideLoadingHUD];
    [self.navigationController popViewControllerAnimated:YES];
}

@end
